var client = require("../message/message.js");
const app = getApp();



PokeLabel = [
  '3',
  '4',
  '5',
  '6',
  '7',
  '8',
  '9',
  '10',
  'J',
  'Q',
  'K',
  'A',
  '2',
]

Page({
  data: {
    // 容器坐标（示例值）
    x1: 30,
    y1: 200,
    x2: 430,
    y2: 400,
    iconSize: 20, // 图片尺寸（单位rpx）

    index_temp : 2,

    //牌面图案索引
    PokeIcon : [
      '../images/001.jpg',
      '../images/002.jpg',
      '../images/003.jpg',
      '../images/004.jpg',
    ],

    // 按钮数据
    buttons: [
      { 
        id: 1, 
        card:{
          index:0,
          label:'1',
          flowerColor:0,
        } 
      },
    ],
    
    // 计算属性
    gap: 20, // 按钮间距
    buttonWidth: 0
  },

  
  onChange(event) {
    this.calculateLayout()
  },

  onLoad() {

    let color = 0

    
    this.data.buttons[0].id = 1;
    this.data.buttons[0].card.index = 3
    this.data.buttons[0].card.label = 'A';
    this.data.buttons[0].card.flowerColor = color
    this.setData({
      buttons:this.data.buttons
    })
    this.calculateLayout()
  },

  getIndexFromTitle(e){
    let label = e.label
    let color = e.color
    let title = 0
    if(label>'2' && label <= '9')
    {
      title = parseInt(label) - 2
    }
    else
    {
      switch(title){
        case '10' : title = 8;break;
        case 'J' : title = 9;break;
        case 'Q' : title = 10;break;
        case 'K' : title = 11;break;
        case 'A' : title = 12;break;
        case '2' : title = 13;break;
        case ' ' : title = 0;break;
      }
    }
    let index = (title - 1) * 4 + (color - 1)
    return index
  },

  getColorLabelFromIndex(index)
  {
    let poke = []
    if(index == 54)
      poke.label = 'joker'
    else if(index == 55)
      poke.label = 'Joker'
    else
    {
      poke.label = PokeLabel[index / 4]
      poke.flowerColor = index % 4
    }
  },

  addTest(e){
    a = {index:this.data.index_temp}
    
    this.addCard(a);
    this.data.index_temp++;
  },

  addCard(e){
    //需要进行排序和id重排
    
    // let id = this.data.buttons.length + 1
    let newCard = {index:e.index,label:e.index,flowerColor:1,}
    const newItem = {id:0,card:newCard};

    
    let arrayPoke = this.data.buttons;

    
    //判断插入位置
    for (let i = 0; i < arrayPoke.length; i++) {
      if(e.index < arrayPoke[i].card.index)
      {
        arrayPoke.splice(i,0,newItem)
        break;
      }
      if(i == arrayPoke.length - 1)
      {
        arrayPoke.splice(arrayPoke.length,0,newItem)
        break;
      }
    }


    //id重排
    for (let i = 0; i < arrayPoke.length; i++) {
      arrayPoke[i].id = i + 1;
    }
    console.log(arrayPoke)
    

    this.setData({
      buttons:arrayPoke
    })
    // console.log(this.data.buttons)
    this.calculateLayout()
  },

  // 删除按钮（示例方法）
  deleteButton(id) {
    const buttons = this.data.buttons.filter(btn => btn.id !== id)
    this.setData({ buttons }, () => {
      this.calculateLayout() // 重新计算布局
    })
  },

  calculateLayout() {
    const { x1, x2, y1, y2, buttons } = this.data
    const containerWidth = x2 - x1
    const containerHeight = y2 - y1
    const buttonHeight = containerHeight * 1 / 3; // 新增高度计算
    const buttonWidth = 50;
    const gap = (buttons.length===0) ? 0 :
    Math.max((containerWidth - buttonWidth * buttons.length) / (buttons.length + 1));
    // const buttonWidth = (containerWidth - gap * (buttons.length - 1)) / buttons.length
    const test_data = containerWidth

    console.log("data = " + test_data + "   gap = " + gap)
    this.setData({
      containerWidth,
      containerHeight,
      buttonHeight,
      buttonWidth,
      gap
    })
  },

  // 切换按钮状态
  toggleButton(e) {
    const id = e.currentTarget.dataset.id
    const buttons = this.data.buttons.map(btn => {
      if (btn.id === id) {
        return { ...btn, isLifted: !btn.isLifted }
      }
      else{
        return { ...btn, isLifted: false }
      }
    })
    this.setData({ buttons })
    console.log("id = " + id)
  }

  

});

function updata_data() {
  this.data.filter_continue_h = 0
}


